package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SecurityCulturalActivityLedgerMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SecurityCulturalActivityLedger;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityCulturalActivityLedgerQO;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityCulturalActivityLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:23
 */
@Service
public class SecurityCulturalActivityLedgerService extends ServiceImpl<SecurityCulturalActivityLedgerMapper, SecurityCulturalActivityLedger> implements IService<SecurityCulturalActivityLedger> {
    @Autowired
    private SecurityCulturalActivityLedgerMapper securityCulturalActivityLedgerMapper;

    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<SecurityCulturalActivityLedgerVO> getListByQO(SecurityCulturalActivityLedgerQO securityCulturalActivityLedgerQO) {
        Page<SecurityCulturalActivityLedgerVO> page = new Page<SecurityCulturalActivityLedgerVO>(securityCulturalActivityLedgerQO.getCurrent(), securityCulturalActivityLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<SecurityCulturalActivityLedgerVO> ans = securityCulturalActivityLedgerMapper.selectByQO(page, securityCulturalActivityLedgerQO);
        for (SecurityCulturalActivityLedgerVO record : ans.getRecords()) {

            String[] ids_s = record.getParticipateOrganizationIds().split(",");
            List<String> participateOrganizationNames = Arrays.stream(ids_s).map(id -> {
                Long id_l = Long.parseLong(id);
                Organization tmp = organizationMapper.selectById(id_l);
                return tmp != null ? tmp.getName() : "该部门已被删除";
            }).collect(Collectors.toList());
            record.setParticipateOrganizationNames(StringUtils.concatStrings(participateOrganizationNames));
        }
        return ans;
    }

    public SecurityCulturalActivityLedgerVO getVOById(Long id) {
        SecurityCulturalActivityLedger ledger = securityCulturalActivityLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        SecurityCulturalActivityLedgerVO vo = new SecurityCulturalActivityLedgerVO();
        BeanUtils.copyProperties(ledger, vo);
        SysUser user = sysUserMapper.selectById(vo.getCreatorId());
        vo.setCreatorName(user == null ? "该用户已被删除" : user.getName());
        String[] split = vo.getParticipateOrganizationIds().split(",");
        List<String> names = Arrays.stream(split).map(t -> {
            Organization tmp = organizationMapper.selectById(t);
            return tmp == null ? "该部门已被删除" : tmp.getName();
        }).collect(Collectors.toList());
        vo.setParticipateOrganizationNames(StringUtils.concatStrings(names));
        return vo;
    }
}
